# 登录后
import json
import sys
import time
from bs4 import BeautifulSoup

from selenium import webdriver  # # 驱动浏览器
from selenium.webdriver.common.by import By  # 选择器
from selenium.webdriver.common.keys import Keys  # 按键
from selenium.webdriver.support.wait import WebDriverWait  # 等待页面加载完毕，寻找某些元素
from selenium.webdriver.support import expected_conditions as EC  ##等待指定标签加载完毕
from selenium.common.exceptions import TimeoutException, NoSuchElementException


class Spider():
    def __init__(self):
        # 登录url
        self.url = 'https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F%3Fcu%3Dtrue%26utm_source%3Dbaidu-pinzhuan%26utm_medium%3Dcpc%26utm_campaign%3Dt_288551095_baidupinzhuan%26utm_term%3D0f3d30c8dba7459bb52f2eb5eba8ac7d_0_249c82c8de624a9a905df73fdd08974e'
        # 设为开发者模式 避免被监测  配置文件对象
        self.options = webdriver.ChromeOptions()
        self.options.add_experimental_option('excludeSwitches', ['enable-automation'])
        self.browser = webdriver.Chrome(options=self.options)

    # 访问京东
    def get_jd(self):
        self.browser.get(self.url)

        # 点击QQ
        self.browser.find_element_by_xpath('//*[@id="kbCoagent"]/ul/li[1]/a/span').click()
        # time.sleep(1)

        # 等待内嵌元素
        wait = WebDriverWait(self.browser,280)
        wait.until(EC.presence_of_element_located((By.ID,'ptlogin_iframe')))

        # 切换至内嵌页面
        self.browser.switch_to.frame('ptlogin_iframe')

        # time.sleep(2)
        self.browser.find_element_by_id('bottom_qlogin').click()  # 点击账号密码登录

        # 找到账号和密码输入框
        ueser_text = self.browser.find_element_by_xpath('//*[@id="u"]') # 账号
        password_text = self.browser.find_element_by_xpath('//*[@id="p"]') #密码
        ueser_text.send_keys('1919404416')  # 输入账号
        password_text.send_keys('lhdtyy1021')  # 输入密码
        # 点击授权并登录
        self.browser.find_element_by_xpath('//*[@id="login_button"]').click()
        time.sleep(1)



    # 获取源码
    def get_data(self):
        print(self.browser.page_source)


    # 解析内容
    def parse_data(self, data):
        pass


if __name__ == '__main__':
    a = Spider()
    a.get_jd()
    a.get_data()
